package cn.cs.service;


import cn.cs.message.resp.TextMessage;
import cn.cs.util.CommonUtil;
import cn.cs.util.MessageUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

//import org.wx.dao.impl.AutoReplyDaoImpl;


/**
 * 核心服务类
 * 
 * @author
 * @date 2013-05-20
 */
@Service
public class CoreService {

	/**
	 * 处理微信发来的请求
	 * 
	 * @param request
	 * @return
	 */
	public static String processRequest(HttpServletRequest request) {
		org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(CoreService.class);
		String respMessage = null;
		String respContent = null;
		try {
			// xml请求解析
			Map<String, String> requestMap = MessageUtil.parseXml(request);
			System.out.println(requestMap.toString());
			// 发送方帐号（open_id）
			String fromUserName = requestMap.get("FromUserName");
			// 公众帐号 （分别为订阅号，服务号，企业号 ）
			String toUserName = requestMap.get("ToUserName");
			// 消息类型
			String msgType = requestMap.get("MsgType");
 			// 回复消息XML
			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(fromUserName);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);



			// 事件推送
			 if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
				// 事件类型
				String eventType = requestMap.get("Event");
				if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {// 关注公众号
					//保存关注者的用户信息
					// WeiXinUserInfoManager.setWeiXinUserInfo(fromUserName);
					 String content ="";
					 if(CommonUtil.isNull(content)){
						 //content="谢谢您的关注！<a href='http://www.zzzsyh.com/index.html#/login'>请先绑定您的手机号</a>";
						 content="感谢关注主事丫环家政服务平台！“主事丫环”是一家专业从事互联网家政服务的平台。我们犹如古时贴心丫环一样为您提供高效便捷的电器安装、维修、清洗、保养等一站式服务。\n" +
								 "主事丫环，贴心管家\n" +
								 "主人，赶紧开始体验吧~~~\n" +
								 "小丫一直在呦>>>\n" +
								 "\n"+
								 "<a href='http://www.zzzsyh.com/index.html#/first'>家电报修</a>\n" +
								 " \n"+
								 "<a href='https://www.zzzsyh.com/index.html#/joining'>加盟合作</a>\n" +
								 " \n"+
								 "<a href='tel:4009615811'>投诉建议</a>\n"+
								 " \n";
					 }
					 textMessage.setContent(content);
					 respMessage = MessageUtil.messageToXml(textMessage);
				}else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {// 取消关注公众号
					//删除某条关注者信息
					//WeiXinUserInfoManager.delWeiXinUserInfo(fromUserName);
					// TODO 取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息
				}
				// 自定义菜单点击事件
				/*else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
					String eventKey = requestMap.get("EventKey");
					//分期流程：查征信→核保→银行电话核实→银行一审→垫款提车→上牌抵押→银行二审
					if(eventKey.equals("xinchefenqi")){
						textMessage.setContent("分期流程：查征信→核保→银行电话核实→银行一审→垫款提车→上牌抵押→银行二审");
						respMessage = MessageUtil.messageToXml(textMessage);
					}else if(eventKey.equals("1")){
						//图文消息
						NewsMessage newsMessage=new NewsMessage();
						newsMessage.setToUserName(fromUserName);
						newsMessage.setFromUserName(toUserName);
						newsMessage.setCreateTime(new Date().getTime());
						newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
						
						//图文消息个数，限制为10条以内
//						newsMessage.setArticleCount(1);

						Article article=new Article();
						//图文内容
						article.setTitle("微支付");
						article.setDescription("点击图片进入");
						article.setPicUrl("http://www.shangfabao.com/wx/images/xlbt.jpg");
						article.setUrl("http://www.shangfabao.com/wx/mainServlet?openId="+fromUserName+"");
						List<Article> ArticleList=new ArrayList<Article>();
						ArticleList.add(article);
						
						newsMessage.setArticleCount(ArticleList.size());
						newsMessage.setArticles(ArticleList);
						respMessage=MessageUtil.messageToXml(newsMessage);
					}*/
					/*if (eventKey.equals("11")) {
						Article article = new Article();
						article.setTitle("会员卡绑定");
						article.setDescription("点击此处进行会员绑定");
						article.setUrl("http://115.28.14.114/wx/updcard.jsp?openId="+fromUserName+"");
						List<Article> articleList = new ArrayList<Article>();
						articleList.add(article);
						// 回复图文消息
						NewsMessage newsMessage = new NewsMessage();
						newsMessage.setToUserName(fromUserName);
						newsMessage.setFromUserName(toUserName);
						newsMessage.setCreateTime(new Date().getTime());
						newsMessage
								.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
						newsMessage.setArticleCount(articleList.size());
						newsMessage.setArticles(articleList);
						respMessage = MessageUtil.messageToXml(newsMessage);
					} else if (eventKey.equals("12")) {	
						respContent=CardDaoImpl.findByUserCard(fromUserName);
						textMessage.setContent(respContent);
						respMessage = MessageUtil.messageToXml(textMessage);
					}*/
				/*	if(eventKey.equals("12")){
						LinkMessage linkMessage = new LinkMessage();
						linkMessage.setUrl("http://www.baidu.com.cn");
					}

				}*/
			 else if(eventType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)||eventType.equals("LOCATION")){
					//纬度
					String latitude = requestMap.get("Latitude");
					//经度
					String longitude = requestMap.get("Longitude");
					//精度
					String precision = requestMap.get("Precision");
					//WeiXinUserInfoManager.updateWeiXinUserJwd(fromUserName,latitude,longitude,precision);
				}
			}
			 // 文本消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
				
				String keyContent=requestMap.get("Content");
				//String xml = new AutoReplyDaoImpl().getAutoReplay("text", toUserName, fromUserName, keyContent);
				//textMessage.setContent("您发送的是文本消息！");
				//respMessage = xml;
				//System.out.println("============"+respMessage);
				//加入A标签
                 /*if(keyContent.equals("手机号")){
                     textMessage.setContent("<a href=\"http://www.baidu.com\">请先绑定手机号！</a>");
                     respMessage = MessageUtil.messageToXml(textMessage);
                 }*/
				textMessage.setContent("感谢您的支持，稍后会有相关人员进行回复！");
				textMessage.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_SERVICE);
				textMessage.setToUserName(fromUserName);
				textMessage.setFromUserName(toUserName);
				textMessage.setCreateTime(System.currentTimeMillis());
				respMessage = MessageUtil.messageToXml(textMessage);
				System.out.println("customerservice::::::"+respMessage);
				logger.info("customerservice::::::logger:::::"+respMessage);
				//以下注释为单多图文的实现
				/*Article article = new Article();
				article.setTitle("多图文");
				article.setDescription("单图文测试");
				article.setUrl("http://www.shangfabao.com/wx/jsp/order/carclean/order.jsp");
				article.setPicUrl("http://wx.qlogo.cn/mmopen/JVukKjzib6SWUxAcRCsicIWEEYRiaKictOMhBBcXAyRZNPlMthF1mRX6V81fOgrfO7u75NDtIjsX0BkS3nOiaIkdYXYQxzoMJvic7Y/0");
				
				Article article2 = new Article();
				article2.setTitle("多图文");
				article2.setDescription("多图文测试");
				article2.setUrl("http://www.shangfabao.com/wx/jsp/order/carclean/order.jsp");
				article2.setPicUrl("http://wx.qlogo.cn/mmopen/JVukKjzib6SWUxAcRCsicIWEEYRiaKictOMhBBcXAyRZNPlMthF1mRX6V81fOgrfO7u75NDtIjsX0BkS3nOiaIkdYXYQxzoMJvic7Y/0");
				List<Article> articleList = new ArrayList<Article>();
				articleList.add(article);
				articleList.add(article2);
				// 回复图文消息
				NewsMessage newsMessage = new NewsMessage();
				newsMessage.setToUserName(fromUserName);
				newsMessage.setFromUserName(toUserName);
				newsMessage.setCreateTime(new Date().getTime());
				newsMessage
						.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
				newsMessage.setArticleCount(articleList.size());
				newsMessage.setArticles(articleList);
				respMessage = MessageUtil.messageToXml(newsMessage);*/
			}

			// 图片消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
				//textMessage.setContent("听的我一头雾水，阁下真是渊博啊，我需要膜拜~");
				 textMessage.setContent("感谢您的支持，稍后会有相关人员进行回复！");
				 respMessage = MessageUtil.messageToXml(textMessage);
			}
			// 地理位置消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
				 textMessage.setContent("您发送的是地理位置消息！");
				 respMessage = MessageUtil.messageToXml(textMessage);
			}
			// 链接消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
				 textMessage.setContent("您发送的是链接消息！");
				 respMessage = MessageUtil.messageToXml(textMessage);
			}
			// 音频消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
				 textMessage.setContent("您发送的是音频消息！");
				respMessage = MessageUtil.messageToXml(textMessage);
			} 
			 ////这里有多客服消息  代码暂未写 稍后考虑
			else if(msgType.equals(MessageUtil. REQ_MESSAGE_TYPE_SERVICE)){
				textMessage.setContent("客服消息");
				respMessage = MessageUtil.messageToXml(textMessage);
			}
			else {
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respMessage;
	}
}
